﻿using System;
using System.Collections.Generic;
using System.Linq;
using Templates;
using Templates.Logic;
using Templates.Logic.Base;
using Templates.Serialization;
using WebService.Nafania.BussinesLayer.Model.Generator;
using WebService.Nafania.DataLayer.Model;
using WebService.Nafania.ViewLayer.BussinessLayer;
using WebService.Nafania.ViewLayer.Model;

namespace WebService.Nafania.BussinesLayer
{
	public class BlockDataView
	{
		private readonly string _alias;
		private readonly string _name;
		private readonly string _key;
		private long? _version;

		private readonly T4TemplateBase _obj;

		private IT4Serializer _serializer;

		private IT4Serializer Serializer
		{
			get { return _serializer ?? (_serializer = new T4Serializer()); }
		}

		public BlockDataView(string key, string alias, string name, long? version)
		{
			_alias = alias;
			_name = name;
			_key = key;


			Template template = TemplateRepository.AsyncAccess.GetByName(alias, name, version);
			if (template != null)
			{
				_obj = (T4TemplateBase) Serializer.Deserialize(template.Value, template.Type);
				_version = template.Version;
			}
		}

		public LineResult GetVersionObject()
		{
			if (_obj == null)
				return null;

			string text = _obj.ToString();

			Block block = new Region();

			int totalLines = block.LineArray(text).Count();

			List<BlockLine> lines = (
			                        	from guid in _obj.BlockIds
			                        	let region = block.FindBlock(text, guid.ToString())
			                        	where region != null
			                        	select new BlockLine
			                        	       	{
			                        	       		Begin = region.BeginLineNumber,
			                        	       		End = region.EndLineNumber,
			                        	       		BlockId = guid
			                        	       	}).ToList();

			return new LineResult
			       	{
								Alias = _alias,
								Name = _name,
								Version = _version,
			       		Blocks = lines,
			       		Text = text,
			       		LinesCount = totalLines
			       	};
		}

		public BlockInfo GetBlockData(Guid blockId)
		{
			if (_obj == null)
				return null;

			T4TemplateBase block = _obj.GetBlockById(blockId);

			if (block == null)
				return null;

			var blockData = new BlockInfo
			                	{
													Alias = _alias,
													Name = _name,
													Version = _version,
													ViewProperties = T4TemplateBaseView.GetViews(block)
			                	};


			return blockData;
		}

		public BlockInfo SaveBlockData(KeyValuePair[] pairs)
		{
			if (_obj == null)
				return null;

			Guid blockId = pairs.GetGuidValueByKey("Id");

			T4TemplateBase block = _obj.GetBlockById(blockId);

			T4TemplateBaseView.UpdateValues(block, pairs);

			_version = TemplateRepository.AsyncAccess.AddTemplate(_key, _alias, _name, Serializer.Type, Serializer.Serialize(_obj)).Version;

			return GetBlockData(block.Id);
		}
	}
}